import 'package:flutter/material.dart';
import 'package:xws/models/notify/chat_msg_obj.dart';
import 'package:xws/state/user_state.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/utils/tools_util.dart';
import 'package:xws/widgets/app_img.dart';
import 'package:xws/widgets/get_size.dart';

/// 消息内容项
class MsgItem extends StatelessWidget {
  const MsgItem({
    Key? key,
    required this.msgObj,
    required this.myHeader,
    required this.otherHeader,
    this.showDate = false,
  }) : super(key: key);

  final ChatMegObj msgObj;
  final String myHeader;
  final String otherHeader;
  final bool showDate;

  void _viewImg(BuildContext context, String url, String hero) {
    assetsViewer(context, <String>[url], heroTag: hero);
    unFocus(context);
  }

  @override
  Widget build(BuildContext context) {
    final bool isMe = msgObj.userId == us.info.id;

    Widget child = Row(
      mainAxisAlignment: isMe ? MainAxisAlignment.end : MainAxisAlignment.start,
      children: <Widget>[
        Container(
          constraints: BoxConstraints(
            maxWidth: sw * 0.65,
            maxHeight: sh,
          ),
          child: msgObj.messageType == MsgType.TEXT ? _buildText(context, isMe) : _buildImg(context, isMe),
          margin: const EdgeInsets.only(top: 30),
          clipBehavior: Clip.antiAlias,
          decoration: BoxDecoration(
            color: isMe ? white(context) : theme(context),
            borderRadius: BorderRadius.circular(14).copyWith(
              topLeft: isMe ? null : const Radius.circular(2),
              topRight: !isMe ? null : const Radius.circular(2),
            ),
          ),
        ),
      ],
    );

    if (showDate)
      child = Column(
        mainAxisSize: MainAxisSize.min,
        children: <Widget>[
          const SizedBox(height: 30),
          Text(
            msgObj.dateStr,
            style: TextStyle(
              fontSize: 12,
              color: disable(context),
            ),
          ),
          child,
        ],
      );

    return child;
  }

  Widget _buildText(BuildContext context, bool isMe) {
    return Padding(
      padding: const EdgeInsets.all(14),
      child: SelectableText(
        msgObj.content,
        style: TextStyle(
          color: isMe ? null : white(context),
          fontSize: 14,
          height: 1.5,
        ),
      ),
    );
  }

  Widget _buildImg(BuildContext context, bool isMe) {
    return GestureDetector(
      onTap: () => _viewImg(context, msgObj.content, msgObj.id),
      child: GetSize(
        onChange: (Size size) {
          if (msgObj.boxHeight == null) {
            msgObj.boxHeight = size.height;
          } else if (msgObj.boxHeight! < size.height) {
            msgObj.boxHeight = size.height;
          }
        },
        child: _buildInnerImg,
      ),
    );
  }

  Widget get _buildInnerImg {
    final Widget img = AppImg(url: msgObj.content, heroTag: msgObj.id + '0');

    if (msgObj.boxHeight == null) return img;

    return SizedBox(
      height: msgObj.boxHeight,
      child: img,
    );
  }
}
